package com.mm.common.utils.theadPoolUtil.strategy.error;

import com.mm.common.utils.theadPoolUtil.BulkExecuteHandler;
import com.mm.common.utils.theadPoolUtil.BulkTask;
import com.mm.common.utils.theadPoolUtil.TaskEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * @Program: 0.test
 * @Author: 海里的鱼
 * @Create: 2024-08-26 20:46
 * @Name: TODO
 * @Desc: TODO
 */
public class RetryAndCollectError implements IErrorStrategy{
    //重试次数
    private int retryCount = 3;
    //间隔时间
    private Long interval = 100L;


    public RetryAndCollectError() {

    }

    public RetryAndCollectError(int retryCount) {
        this.retryCount = retryCount;
    }


    public RetryAndCollectError(int retryCount, Long interval) {
        this.retryCount = retryCount;
        this.interval = interval;
    }

    @Override
    public void processError(BulkExecuteHandler handler, BulkTask task) {
        for(int i=0; task.getState()!= TaskEnum.SUCCESS && i<retryCount; i++){
            //暂停间隔
            try {
                Thread.sleep(interval);
                handler.addRetryCount();
                handler.errorRetry(task);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        if(task.getState() != TaskEnum.SUCCESS){
            handler.addErrorTask(task.getName());
        }
    }
}
